package com.lagou.homework.graphx

import org.apache.spark.graphx.{Edge, Graph, VertexId, VertexRDD}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object AirportGraphx {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf().setAppName(this.getClass.getCanonicalName.init).setMaster("local[*]")
        val sc: SparkContext = new SparkContext(conf)
        sc.setLogLevel("warn")

        val vertexArray: Array[(VertexId, String)] = Array((1L, "SFO"), (2L, "ORD"), (3L, "DFW"))
        val edgeArray: Array[Edge[Int]] = Array(
            Edge(1L, 2L, 1800),
            Edge(2L, 3L, 800),
            Edge(3L, 1L, 1400))

        //构造vertexRDD和edgeRDD
        val vertexRDD: RDD[(VertexId, String)] = sc.makeRDD(vertexArray)
        val edgeRDD: RDD[Edge[Int]] = sc.makeRDD(edgeArray)

        //构造图
        val graph: Graph[String, Int] = Graph(vertexRDD, edgeRDD)

        //求所有顶点
        println("*************************所有顶点********************************")
        graph.vertices.foreach(println)

        //求所有的边
        println("*************************所有边********************************")
        graph.edges.foreach(println)

        //求所有的triplets
        println("*************************所有triplets********************************")
        graph.triplets.foreach(println)

        //求顶点数
        println(s"顶点数：${graph.vertices.count()}")

        //求边数

        println(s"边数：${graph.edges.count()}")

        //求机场距离大于1000的有几个，有哪些？按所有机场之间的距离排序（降序），输出结果
        val resEdge: RDD[Edge[Int]] = graph.edges.filter { edge => edge.attr > 1000 }
        val nums: VertexId = resEdge.count()
        println(s"机场距离大于1000的有$nums 个")
        println("分别为：")
        resEdge.sortBy(- _.attr).collect().foreach(println)
    }
}
